<?php

class Workaholic
/**
 * Класс Workaholic -- запускает задачи после завершения скрипта.
 * 
 * <code>
 * function a ($x) { var_dump ($x); }
 * Workaholic::addTask('a', array('1'));
 * die();
 * </code>
 *
 * @package RL
 * @author Lucky <lucky@jetstyle.ru> 
 * @version $Id$
 */
{
	static public $jobs = array();
	public function addTask ($callback /*, $arg, $arg1,...*/)
	{
		static $q;
		if (!isset($q)) {
			//header('Connection: close');
			set_time_limit(0);
			ignore_user_abort(true);
			register_shutdown_function(array(__CLASS__, 'run'));
		}
		$args = func_get_args();
		array_shift($args);
		self::$jobs[] = array($callback, $args);
		return true;
	} // end of function addTask

	public function run ()
	{
		//session_write_close();
		//while (@ob_end_flush());
		//flush();
		foreach (self::$jobs as $job) {
			list($callback, $args) = $job;
			debug(__METHOD__."() {$callback[1]}");
			call_user_func_array($callback, $args);
		}
	} // end of function run
}

